package com.aconcagua.samples.charts.state.model {
  import flexunit.framework.TestCase;
  
  import mx.collections.ArrayCollection;

  public class SliderTest extends TestCase {
    private var _startTime: Number;
    private var _endTime: Number;
    private var _slider: Slider;

    public function SliderTest() {
    }

    override public function setUp():void {
      _startTime = new Date().time;
      _endTime = new Date().time + 1000 * 60 * 60 * 24 * 365;

      var elements: ArrayCollection = new ArrayCollection();
      elements.addItem(new SeriesElement(new Date(_startTime), 10));
      elements.addItem(new SeriesElement(new Date(_endTime), 20));
      
      _slider = new Slider(new Series(elements));
    }
    
    override public function tearDown():void {
      _startTime = NaN;
      _endTime = NaN;
      _slider = null;
    }
    
    public function testPointToDate(): void {
      assertEquals("Left boundary test failed", _startTime, _slider.pointToDate(0).time);
      assertEquals("Right boundary test failed", _endTime, _slider.pointToDate(_slider.totalSpan).time);
      assertEquals(_startTime + (_endTime - _startTime) / 2, _slider.pointToDate((_endTime - _startTime) / 2).time);
      assertEquals(_startTime + (_endTime - _startTime) / 3, _slider.pointToDate((_endTime - _startTime) / 3).time);
      assertEquals(Math.floor(_startTime + ((_endTime - _startTime) / 2.34356767889)), _slider.pointToDate((_endTime - _startTime) / 2.34356767889).time);
    }
    
    public function testZoom(): void {
      var zoomFrom: Date = new Date(_startTime + (_endTime - _startTime) / 3);
      var zoomTo: Date = new Date(_startTime + (_endTime - _startTime) * 2 / 3);
      _slider.zoom(zoomFrom.time, zoomTo.time);
      
      assertEquals("Error in leftPeriodSpanPercent", 1 / 3, _slider.leftPeriodSpanPercent);
      assertEquals("Error in zoomPeriodSpanPercent",1 / 3, _slider.zoomPeriodSpanPercent);
      assertEquals("Error in rightPeriodSpanPercent",1 / 3, _slider.rightPeriodSpanPercent);
    }    
    
    public function testZoom2(): void {
      var zoomFrom: Date = new Date(_startTime + (_endTime - _startTime) / 5);
      var zoomTo: Date = new Date(_startTime + (_endTime - _startTime) * 3 / 4);
      _slider.zoom(zoomFrom.time, zoomTo.time);
      
      assertEquals("Error in leftPeriodSpanPercent", 1 / 5, _slider.leftPeriodSpanPercent);
      assertEquals("Error in zoomPeriodSpanPercent", (3 / 4) - (1 / 5), _slider.zoomPeriodSpanPercent);
      assertEquals("Error in rightPeriodSpanPercent",1 - (3 / 4), _slider.rightPeriodSpanPercent);
    }    
  }
}